<?php

includePackage('Video');

class MoviehubWebModule extends WebModule
{
    protected static $defaultModel = 'MoviehubDataModel';
    protected $id='moviehub';
    protected $feeds = array();
    protected $group;
    protected $groups;

    public function getMovieGroups() {
        return $this->getModuleSections('moviegroups');
    }

    protected function getModuleAdminSections() {
        $sections = parent::getModuleAdminSections();

        foreach ($this->getMovieGroups() as $appgroup=>$data) {
            $sections['movie-'.$appgroup] = array(
                'title'=>$data['TITLE'],
                'type'=>'module hidden'
            );
        }

        return $sections;
    }

    protected function getModuleAdminConfig() {
        $configData = parent::getModuleAdminConfig();

        foreach ($this->getMovieGroups() as $appgroup=>$data) {
            $appData = $configData['movie'];
            $appData['TITLE'] = $data['TITLE'];
            $appData['config'] = 'movie-' . $appgroup;
            $appData['configMode'] = Config::OPTION_CREATE_EMPTY;
            $configData['movie-'.$appgroup] = $appData;
        }
        unset($configData['movie']);

        return $configData;
    }

    public function getMovieForGroup($group=null) {
        $file = 'movie-' . $group;
        $filePath = SITE_CONFIG_DIR . DIRECTORY_SEPARATOR . $this->getConfigModule() . DIRECTORY_SEPARATOR . $file . '.ini';

        if (file_exists($filePath))
            return $this->getModuleSections($file);
        else
            return false;
    }

    protected function getMovieData($group=null) {
        $apps = $this->getMovieForGroup($group);
        return $apps;
    }

    protected function initialize() {
        $this->groups = $this->getMovieGroups();
        $this->group = $this->getArg('group');
        if (!array_key_exists($this->group, $this->groups)) {
            $this->group = key($this->groups);
        }
        $this->feeds = $this->getMovieData($this->group);
    }

    protected function getDefaultSection() {
        return key($this->feeds);
    }

    public function linkForItem(KurogoObject $video, $data=null) {

        $options = array(
            'id'=>$video->getID(), 'group' => $this->group
        );

        foreach (array('feed','filter') as $field) {
            if (isset($data[$field])) {
                $options[$field] = $data[$field];
            }
        }

        $addBreadcrumb = isset($data['addBreadcrumb']) ? $data['addBreadcrumb'] : true;
        $noBreadcrumbs = isset($data['noBreadcrumbs']) ? $data['noBreadcrumbs'] : false;

        if ($noBreadcrumbs) {
            $url = $this->buildURL('detail', $options);
        } else {
            $url = $this->buildBreadcrumbURL('detail', $options, $addBreadcrumb);
        }

        $desc = $video->getDescription();
        if (isset($data['federatedSearch']) && $data['federatedSearch']) {
            $subtitle = '';
        } else {
            $subtitle = "(" . VideoModuleUtils::getDuration($video->getDuration()) . ") " . $desc;
        }
        if (!$video->getType()) {
            $subtitle = '';
        }

        return array(
            'url'=>$url,
            'title'=>$video->getTitle(),
            'subtitle'=>$subtitle,
            'imgWidth'=>120,
            'imgHeight'=>100,
            'img'=>$video->getImage(),
            'large'=>true,
        );
    }

    public function searchItems($searchTerms, $limit=null, $options=null) {

        $section = isset($options['feed']) ? $options['feed'] : $this->getDefaultSection();
        $controller = $this->getFeed($section);

        $controller->setLimit($limit);
        $items = $controller->search($searchTerms);

        return $items;
    }

    protected function getFeed($feed=null) {
        $feed = isset($this->feeds[$feed]) ? $feed : $this->getDefaultSection();
        $feedData = $this->feeds[$feed];
        if (isset($feedData['BASE_URL'])) {
            $feedData['PARSER_CLASS'] = 'RSSDataParser';
        }
        $modelClass = isset($feedData['MODEL_CLASS']) ? $feedData['MODEL_CLASS'] : self::$defaultModel;
        $controller = MoviehubDataModel::factory($modelClass, $feedData);

        return $controller;
    }

    protected function initializeForPage() {

        if ($this->pagetype == 'basic') {
            $this->setTemplatePage('videoError.tpl');
            return;
        }

        if (count($this->feeds) == 0 || count($this->groups) == 0) {
            throw new KurogoConfigurationException("No video feeds configured");
        }

        // Categories / Sections

        $section = $this->getArg(array('feed', 'section'), $this->getDefaultSection());
        if (!isset($this->feeds[$section])) {
            $section = $this->getDefaultSection();
        }

        $this->assign('currentSection', $section);
        $this->assign('sections'      , VideoModuleUtils::getSectionsFromFeeds($this->feeds));

        $controller = $this->getFeed($section);

        switch ($this->page) {
            case 'pane':
                if ($this->ajaxContentLoad) {
                    $start = 0;
                    $maxPerPage = $this->getOptionalModuleVar('MAX_PANE_RESULTS', 5);
                    $data = array(
                        'noBreadcrumbs' => true,
                        'feed' => $section
                    );

                    $controller->setStart($start);
                    $controller->setLimit($maxPerPage);
                    $items = $controller->items();
                    $videos = array();

                    foreach ($items as $video) {
                        $videoLink = $this->linkForItem($video, $data);

                        $videoLink['url'] = $this->buildURL('index', array('group' => $this->group), false).
                            '#'.urlencode(FULL_URL_PREFIX.ltrim($videoLink['url'], '/'));
                        $videoLink['img'] = $video->getStillFrameImage();
                        unset($videoLink['imgWidth']);
                        unset($videoLink['imgHeight']);

                        $videos[] = $videoLink;
                    }

                    $this->assign('stories', $videos);
                }
                $this->addInternalJavascript('/common/javascript/lib/ellipsizer.js');
                $this->addInternalJavascript('/common/javascript/lib/paneStories.js');
                break;

            case 'group';
                foreach($this->getMovieGroups() as $feed => $feedData){
                    $list['title'] = $feedData['TITLE'];
                    $list['url'] = $this->buildBreadcrumbURL('index', array('group' => $feed), true);
                    $listArr[] = $list;
                }
                $this->assign('listArr', $listArr);
                break;

            case 'search':
            case 'index':
                $this->group = $this->getArg(array('group'), '');
                if($this->group == '' && count($this->groups) > 1) {
                    $this->redirectTo('group', array(), true);
                }
                $maxPerPage = $this->getOptionalModuleVar('MAX_RESULTS', 10);
                $start = $this->getArg('start', 0);
                $controller->setStart($start);
                $controller->setLimit($maxPerPage);

                if ($this->page == 'search') {
                    if ($filter = $this->getArg('filter')) {
                        $searchTerms = trim($filter);
                        $this->setLogData($searchTerms);
                        $items = $controller->search($searchTerms);
                        $this->assign('searchTerms', $searchTerms);
                    } else {
                        $this->redirectTo('index', array('feed' => $section), false);
                    }
                } else {
                    $this->setLogData($section, $controller->getTitle());
                    $items = $controller->items();
                }

                $totalItems = $controller->getTotalItems();
                $videos = array();
                foreach ($items as $video) {
                    $videos[] = $this->linkForItem($video, array('feed' => $section));
                }

                $this->assign('videos', $videos);
                $this->assign('totalItems', $totalItems);

                $previousURL = null;
                $nextURL = null;

                if ($totalItems > $maxPerPage) {
                    $args = $this->args;

                    if ($start > 0) {
                        $args['start'] = $start - $maxPerPage;
                        $previousURL = $this->buildBreadcrumbURL($this->page, $args, false);
                    }

                    if (($totalItems - $start) > $maxPerPage) {
                        $args['start'] = $start + $maxPerPage;
                        $nextURL = $this->buildBreadcrumbURL($this->page, $args, false);
                    }
                }

                /* only assign section if it's the search page, otherwise section comes from select box */
                if ($this->page == 'search') {
                    $hiddenArgs = array(
                        'feed' => $section, 'group' => $this->group
                    );
                } else {
                    $hiddenArgs = array('group' => $this->group);
                }

                $this->addInternalJavascript('/common/javascript/lib/ellipsizer.js');
                $this->addOnLoad('setupVideosListing();');

                $this->assign('placeholder', $this->getLocalizedString('SEARCH_MODULE', $this->getModuleName()));
                $this->assign('groupname',   $this->group);
                $this->assign('start',       $start);
                $this->assign('previousURL', $previousURL);
                $this->assign('nextURL',     $nextURL);
                $this->assign('hiddenArgs',  $hiddenArgs);
                $this->assign('maxPerPage',  $maxPerPage);

                if ($this->getOptionalModuleVar('BOOKMARKS_ENABLED', 1)) {
                    $this->generateBookmarkLink();
                }
                break;

            case 'bookmarks':
                if (!$this->getOptionalModuleVar('BOOKMARKS_ENABLED', 1)) {
                    $this->redirectTo('index');
                }

                $controllerCache = array(
                    $section => $controller,
                );
                $videos = array();

                foreach ($this->getBookmarks() as $aBookmark) {
                    if (!$aBookmark) { continue; }

                    parse_str(stripslashes($aBookmark), $params);
                    if (isset($params['feed'], $this->feeds[$params['feed']], $params['id'])) {
                        if (!isset($controllerCache[$params['feed']])) {
                            $controllerCache[$params['feed']] = $this->getFeed($params['feed']);
                        }

                        if ($video = $controllerCache[$params['feed']]->getItem($params['id'])) {
                            $videos[] = $this->linkForItem($video, $params);
                        }
                    }
                }
                $this->assign('videos', $videos);

                $this->addInternalJavascript('/common/javascript/lib/ellipsizer.js');
                $this->addOnLoad('setupVideosListing();');
                break;

            case 'player':
                $videoid = $this->getArg(array('id','videoid'));
                if ($video = $controller->getItem($videoid)) {
                    $this->assign('videoObject',      $video);
                }
                break;
            case 'detail':
                $videoid = $this->getArg(array('id', 'videoid'));

                if ($video = $controller->getItem($videoid)) {
                    $this->setLogData($videoid, $video->getTitle());
                    if (!$video->getType()) {
                        $feed = isset($this->feeds[$section]) ? $section : $this->getDefaultSection();
                        $feedData = $this->feeds[$feed];
                        $feedData['DEVICE'] = $this->platform;
                        $feedData['BASE_URL'] = $video->getUrl();
                        $feedData['PARSER_CLASS'] = 'MoviehubDataParser';
                        $modelClass = isset($feedData['MODEL_CLASS']) ? $feedData['MODEL_CLASS'] : self::$defaultModel;
                        $controller = MoviehubDataModel::factory($modelClass, $feedData);
                        $arr = $controller->items();
                        $video->setUrl($arr['src']);
                        $video->setType($arr['type']);
                    }

                    $this->assign('videoTitle',       $video->getTitle());
                    $this->assign('videoDescription', $video->getDescription());
                    $this->assign('videoAuthor'     , $video->getAuthor());
                    $this->assign('videoDate'       , $video->getPublished()->format('M j, Y'));
                    $this->assign('videoObject',      $video);

                    $body = $video->getDescription() . "\n\n" . $video->getURL();

                    if ($this->getOptionalModuleVar('SHARING_ENABLED', 1)) {
                        $this->assign('shareEmailURL', $this->buildMailToLink("", $video->getTitle(), $body));
                        $this->assign('shareTitle',    $this->getLocalizedString('VIDEO_SHARE_MESSAGE'));
                        $this->assign('shareURL',      $video->getURL());
                        $this->assign('shareRemark',   $video->getTitle());
                    }

                      // Bookmark
                    if ($this->getOptionalModuleVar('BOOKMARKS_ENABLED', 1)) {
                        $cookieParams = array(
                            'feed'  => $section,
                            'id'    => $videoid
                        );

                        $cookieID = http_build_query($cookieParams);
                        $this->generateBookmarkOptions($cookieID);
                    }
                } else {
                    $this->redirectTo('index', array('feed'=>$section),false);
                }
                break;
        }
    }
}
